package jp.seraph.same.view;

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jp.seraph.same.controller.PoseController;
import jp.seraph.same.model.JointModel;

public class JointSlider extends JSlider {
    public JointSlider(JointModel aModel, PoseController aController) {
        mController = aController;
        this.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if (mModel == null)
                    return;

                JointSlider tSender = (JointSlider) e.getSource();
                mModel.setJointAngleAsDegree(tSender.getValue());
                if (mUpdate)
                    mController.updatePose();
            }
        });

        this.setModel(aModel);
    }

    private JointModel mModel;
    private PoseController mController;
    private boolean mUpdate = true;
    private static final long serialVersionUID = 10L;

    public void setModel(JointModel aModel) {
        mModel = aModel;
        double tMax = aModel.getMaxAngleAsDegree();
        double tMin = aModel.getMinAngleAsDegree();
        double tValue = aModel.getJointAngleAsDegree();
        this.setMaximum((int) tMax);
        this.setMinimum((int) tMin);
        this.setValue((int) tValue);
        int tSpacing = (int) ((tMax - tMin) / 3);
        this.setMajorTickSpacing(tSpacing);
        this.setLabelTable(this.createStandardLabels(tSpacing));
        this.setPaintLabels(true);
        this.setPaintTicks(true);
    }

    public void setRealTimeUpdate(boolean aUpdate) {
        if (aUpdate) {
            mController.updatePose();
        }
        mUpdate = aUpdate;
    }
}
